package com.ruoyi.web.controller.module.material;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.material.domain.Handbook;
import com.ruoyi.material.service.IHandbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 礼物图鉴手册信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/material/handbook")
public class HandbookController extends BaseController
{
    @Autowired
    private IHandbookService handbookService;

    /**
     * 获取礼物图鉴手册列表 - 所有
     */
    @PostMapping("/handbookList")
    public TableDataInfo list(@RequestBody Handbook handbook)
    {
        startPageByPost();
        List<Handbook> list = handbookService.selectHandbookList(handbook);
        return getDataTable(list);
    }

    @Log(title = "礼物图鉴手册管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('material:handbook:export')")
    @PostMapping("/handbookExport")
    public void export(HttpServletResponse response, Handbook handbook)
    {
        List<Handbook> list = handbookService.selectHandbookList(handbook);
        ExcelUtil<Handbook> util = new ExcelUtil<Handbook>(Handbook.class);
        util.exportExcel(response, list, "礼物图鉴手册数据");
    }

    @PostMapping("/handbookImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Handbook> util = new ExcelUtil<Handbook>(Handbook.class);
        util.importTemplateExcel(response, "礼物图鉴手册数据");
    }

    /**
     * 根据礼物图鉴手册编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:handbook:query')")
    @GetMapping(value = { "/", "/{handbookId}" })
    public AjaxResult getInfo(@PathVariable(value = "handbookId", required = false) Long handbookId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(handbookId))
        {
            Handbook sysHandbook = handbookService.selectHandbookById(handbookId);
            ajax.put(AjaxResult.DATA_TAG, sysHandbook);
        }
        return ajax;
    }

    /**
     * 新增礼物图鉴手册
     */
    @PreAuthorize("@ss.hasPermi('material:handbook:add')")
    @Log(title = "礼物图鉴手册管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Handbook handbook, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            handbook.setShopId(shopId);
        }
        startPageByPost();
        handbook.setCreateBy(getUsername());
        return toAjax(handbookService.insertHandbook(handbook));
    }

    /**
     * 修改礼物图鉴手册
     */
    @PreAuthorize("@ss.hasPermi('material:handbook:edit')")
    @Log(title = "礼物图鉴手册管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Handbook handbook)
    {
        handbook.setUpdateBy(getUsername());
        return toAjax(handbookService.updateHandbook(handbook));
    }

    /**
     * 删除礼物图鉴手册
     */
    @PreAuthorize("@ss.hasPermi('material:handbook:remove')")
    @Log(title = "礼物图鉴手册管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{handbookIds}")
    public AjaxResult remove(@PathVariable Long[] handbookIds)
    {
        return toAjax(handbookService.deleteHandbookByIds(handbookIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('material:handbook:edit')")
    @Log(title = "礼物图鉴手册管理 - 状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/handbookChangeStatus")
    public AjaxResult changeStatus(@RequestBody Handbook handbook)
    {
        handbook.setUpdateBy(getUsername());
        return toAjax(handbookService.updateHandbookStatus(handbook));
    }
}
